<template>
  <customTree  :data="treeData"   :dataAll="dataAll" @click="click"  :checkable="true"  :subHeight="280" ref="customTree" :propCheckIdList="checkIdList"/>
</template>

<script>

export default {
  desc: '角色所拥有用户树',
  name: 'treeRoleUser',
  components: {
  },
  props: ['checkIdList'],
  created() {
    this.load()
  },
  data() {
    return {
      treeData: [],
      dataAll: [],
    }
  },
  methods: {
    // 加载数据
    load() {
      this.$ajax.get('/user/tree', {
        params: {
          roleId: this.roleId
        }
      }).then(response => {
        if (response.code == 0) {
          this.dataAll = response.data
          this.treeData = global.utils.convertToTree(response.data, {
            rootNode: {id: 0, parentId: 'none', title: '所有用户'},
            keyFieldName: 'id',
            parentFieldName: 'parentId',
            labelFieldName: 'name'
          })
        }
      })
    },

    // 节点点击时触发
    click(nodeData) {
      this.$emit('click', nodeData)
    },

    // 树实例
    getInst() {
      return this.$refs.customTree
    }
  },
  watch: {
    // 监听表单设置本组件的值
    userId: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        if (this.userId) {
          this.load()
        }
      },
    }
  },
  computed: {
  }
}
</script>

<style lang="scss">
</style>
